package com.hfy.huipaywater.reactNative;

import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.amap.api.services.help.Tip;
import com.amap.api.services.poisearch.PoiSearch;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableArray;
import com.google.gson.Gson;
import com.facebook.react.bridge.Callback;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.poisearch.PoiResult;

import java.util.List;
import java.util.ArrayList;

/**
 * Created by zzy on 2017/6/29.
 */
public class RnTest extends ReactContextBaseJavaModule {
    private PoiSearch.Query query;// Poi查询条件类
    private PoiSearch poiSearch;// POI搜索
    private List<String> listString;
    public RnTest(ReactApplicationContext reactContext) {
        super(reactContext);
    }
    // ReactContextBaseJavaModule要求派生类实现getName方法。这个函数用于返回一个字符串
    // 这个字符串用于在JavaScript端标记这个原生模块
    @Override
    public String getName() {
        return "AMapService";
    }
    @ReactMethod
    public void searchAreaEvent(String newText, String city, final Callback call){
        //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
        InputtipsQuery inputquery = new InputtipsQuery(newText, city);
        inputquery.setCityLimit(true);//限制在当前城市
        Inputtips inputTips = new Inputtips(getReactApplicationContext(), inputquery);
        inputTips.setInputtipsListener(new Inputtips.InputtipsListener() {
            @Override
            public void onGetInputtips(List<Tip> list, int i) {
                if(i==1000){
                    List<PoiItem> mlist=new ArrayList<>();
                    for(Tip tip:list){
                        PoiItem item=new PoiItem();
                        Position poit=new Position();
                        item.setAdCode(tip.getAdcode());
                        item.setAddress(tip.getAddress());
                        item.setDistrict(tip.getDistrict());
                        item.setName(tip.getName());
                        item.setPoiId(tip.getPoiID());
                        item.setTypeCode(tip.getTypeCode());
                        poit.setLatitude(tip.getPoint().getLatitude()+"");
                        poit.setLongitude(tip.getPoint().getLongitude()+"");
                        item.setPoint(poit);
                        mlist.add(item);
                    }
                    Gson gson = new Gson();
                    String params=gson.toJson(mlist);
                    call.invoke(params);
                }
            }
        });
        inputTips.requestInputtipsAsyn();
    }

   @ReactMethod
   public void searchEvent(double latitude, double longitude, final Callback call){
        PoiSearch.Query query = new PoiSearch.Query("", "", "");
        poiSearch = new PoiSearch(getReactApplicationContext(), query);
        poiSearch.setOnPoiSearchListener(new PoiSearch.OnPoiSearchListener() {
            @Override
            public void onPoiSearched(PoiResult poiResult, int i) {
                if(i==1000){
                    List<PoiItem> plist=new ArrayList<>();
                    for(com.amap.api.services.core.PoiItem poitItem:poiResult.getPois()){
                        PoiItem item=new PoiItem();
                        Position poit=new Position();
                        item.setAdCode(poitItem.getAdCode());
                        item.setAddress(poitItem.getSnippet());
                        item.setDistrict(poitItem.getDirection());
                        item.setName(poitItem.getTitle());
                        item.setPoiId(poitItem.getPoiId());
                        item.setTypeCode(poitItem.getTypeCode());
                        poit.setLatitude(poitItem.getLatLonPoint().getLatitude()+"");
                        poit.setLongitude(poitItem.getLatLonPoint().getLongitude()+"");
                        item.setPoint(poit);
                        plist.add(item);
                    }
                    Gson gson = new Gson();
                    String params=gson.toJson(plist);
                    call.invoke(params);
                }
            }

            @Override
            public void onPoiItemSearched(com.amap.api.services.core.PoiItem poiItem, int i) {

            }
        });
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(latitude,
                longitude), 1000));//设置周边搜索的中心点以及半径
        poiSearch.searchPOIAsyn();
    }


}
